<template>
	<view class="Contain"> <!-- Contain Start -->
		<!-- 收货地址 -->
		<view class="address" v-if="useradrInfo.data[0] != undefined" v-for="(item,index) in useradrInfo.data">
			<view class="name">
				<u-row gutter="0">
					<u-col span="1.5" text-align="left">
						<view class="na" style="top:0rpx;">
							<u-checkbox :checked="item.checked" shape="circle" size="26" iconSize="22" activeColor="#2979FF" @change="SelecedAdr(index)"></u-checkbox>	
						</view>
					</u-col>
					<u-col span="9" text-align="left">
						<view class="nb" @click="SelecedAdr(index)">{{item.userName}} {{item.phone}} <text class="defaultadr" v-if="item.isDefault==='默认地址'">默认地址</text></view>
						<view class="nb2" @click="SelecedAdr(index)">{{item.address}}{{item.detailadr}}</view>
					</u-col>
					<u-col span="1.5" text-align="right">
						<view class="nc relative left-2" @click="EditorAdr(index)"><u-icon name="edit-pen" color="#ccc" size="28"></u-icon></view>
					</u-col>
				</u-row>	
			</view> <!-- name End -->
		</view> <!-- address End -->
		<view v-else class="flex justify-center w-[100%]">
			<text class="text-sm text-gray-500">暂无收货地址，点击下方添加</text>
		</view>
		
		<!-- <view><u-button type="primary" :custom-style="btncustomstyle" @click="submit">确定</u-button></view> -->
		<view class="NewAdd"><u-button type="primary" :custom-style="btncustomstyle" @click="AddNewAdr">新增收货地址</u-button></view>
	
	</view> <!-- Contain End -->
</template>

<script setup lang="ts">
import { ref,reactive } from 'vue'
import { GoUrl, Toast } from '@/utils/utils'
import {onShow } from '@dcloudio/uni-app'
import { userStore } from '@/stores/userStore'
const store = userStore()

let userid=ref(0);
let checked=ref(true)
let checked2=ref(false)
let useradrInfo=reactive<any>({data:[
	/* {
		id:1,
		userName:"小菜明明子",
		phone:"13800138000",
		address:"广东省 广州市 天河区 海北大道",
		detailadr:"神掌村神级大厦1001栋201室",
		isDefault:"默认地址",
		checked:false
	},
	{
		id:2,
		userName:"饭团锅锅酱",
		phone:"13800138000",
		address:"广西 柳州区 螺蛳粉大道",
		detailadr:"好食大厦1100栋888室",
		isDefault:"",
		checked:false
	},
	{
		id:3,
		userName:"真的不知道",
		phone:"13800138000",
		address:"湖南 倩倩区 海龙湾 大道",
		detailadr:"飞沙走石大厦122200栋qqq室",
		isDefault:"",
		checked:false
	} */
]});
let Address=ref("") //广东省湛江市赤坎区湛江大道北73丽华花园73号皇后大道东3号
let user=ref("") //马红梅
let phone=ref("") //15812345678
let btncustomstyle=reactive<any>({
		"width":"93%",
		"background":"#3288FD",
		"padding":"10rpx 0",
		"marginTop":"30rpx",
		"color":"#FFFFFF",
		"fontSize":"30rpx",
});
let btncustomstyle2=reactive<any>({
		"width":"93%",
		"background":"#ccc",
		"padding":"10rpx 0",
		"marginTop":"30rpx",
		"color":"#FFFFFF",
		"fontSize":"30rpx",
});

onShow(()=>{
	getUserAdress();
})

const clearStr=(val:string)=>{
	//return val.replace(/\s*/img,"");
	return val;
}


const getUserAdress=async ()=>{
	if(uni.getStorageSync("shopuserinfo")!='' && uni.getStorageSync("shopuserinfo")!=null){
		userid.value=uni.getStorageSync("shopuserinfo").id;
		uni.showLoading({
			title:"正在获取地址"
		})
		setTimeout(()=>{
			uni.hideLoading();
		},3000);
		let paraData={
			userid:userid.value,
			token:uni.getStorageSync('token')
		};
		console.log("收货地址请求参数:",paraData);
		/*
		let res = await getUserAdressData(paraData); //Api
		console.log("获取收货地址",res);
		if(res.data.code===200){
			uni.hideLoading();
			useradrInfo.data=[];
			if(res.data.userAdrList!=null){
				res.data.userAdrList.forEach(item=>{
					var obj={};
					obj.id=item.id;
					obj.userName=item.adrname;
					obj.phone=item.adrphone;
					obj.address=item.address;
					obj.detailadr=item.detailadr;
					obj.isDefault=(item.isdefault!=null && item.isdefault==='默认地址')?"默认地址":"";
					obj.checked=false;
					useradrInfo.data.push(obj);
				});
			}
		}else{
			uni.hideLoading();
		}		
	}else{
		store.backRouter="../shop_selectedAddress_new/shop_selectedAddress_new";
		Toast("请登录后访问222")
		setTimeout(()=>{
			//uni.navigateBack();
			GoUrl("../mine/mine",2);
		},1000);
	}
		//*/
		uni.$tools.Reqeust('/shopApi/getUserAdressData',paraData,'POST').then((res:any)=>{
			console.log("获取收货地址",res);
			if(res.data.code===200){
				uni.hideLoading();
				useradrInfo.data=[];
				if(res.data.myAdrList!=null){
					res.data.myAdrList.forEach(item=>{
						var obj={};
						obj.id=item.id;
						obj.userName=item.adrname;
						obj.phone=item.adrphone;
						obj.address=item.address;
						obj.detailadr=item.detailadr;
						obj.isDefault=(item.isdefault!=null && item.isdefault==='默认地址')?"默认地址":"";
						obj.checked=false;
						useradrInfo.data.push(obj);
					});
				}
			}else{
				uni.$tools.Toast(res.data.msg);
			}
		});
	}
}


const AddNewAdr=()=>{
	uni.removeStorageSync("userAddress");
	GoUrl('../shop_addAddress/shop_addAddress');
}

const EditorAdr=(index:number)=>{
	uni.setStorageSync("userAddress",useradrInfo.data[index]);
	//编辑地址
	GoUrl("../shop_addAddress/shop_addAddress");
}


const SelecedAdr=(index:number)=>{
	clearChecked(); //每次点击清除之前所选
	useradrInfo.data[index].checked=true;
	uni.setStorageSync("userAddress",useradrInfo.data[index]);
	console.log("用户选择了收货地址:",useradrInfo.data[index]);
	//返回
	GoUrl("../shop_order/shop_order",1);
}



const clearChecked=()=>{
	useradrInfo.data.forEach((item:any,index:number)=>{
		useradrInfo.data[index].checked=false;
	})
	console.log("清除之后",useradrInfo);
}

const submit=()=>{
	let index=useradrInfo.data.findIndex((o:any)=>o.checked===true);
	if(index===-1){
		alert("请选择一个收货地址");
		return;
	}
	//选择时已缓存，返回订单页
	GoUrl("../shop_order/shop_order");	
}
</script>
<style>
	page{background-color:#F7F9FD;padding-bottom:200rpx;}
</style>
<style scoped lang="scss" >
	@import "shop_selectedAddress_new.scss";
</style>